﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.MT3DMS.FileLayer;

namespace HA.MT3DMS.DomainLayer
{
    [Serializable]
    public class ChemicalReaction:MTPackage,ICloneable
    {


        public ChemicalReaction()
        {
            this._isoTHM = ISOTHMType.NOSorption;

            this._kineticRateReaction = KineticRateReaction.NONE;


            this._bulkDensity = new List<double[,]>();

            this._porosity2 = new List<double[,]>();

          


            this._ChemicalSpecies = new List<ChemicalSpecies>();

 
        }

        public void SetBulkDensity(List<double[,]> BulkDensity)
        {
            for (int i = 0; i < this.BulkDensity.Count; i++)
            {
                this.BulkDensity[i] = BulkDensity[i];
            }
        }
        public void SetPorosity2(List<double[,]> Porosity2)
        {
            for (int i = 0; i < this.Porosity2.Count; i++)
            {
                this.Porosity2[i] = Porosity2[i];
            }
        }


        ISOTHMType _isoTHM;

        public ISOTHMType IsoTHM
        {
            get { return _isoTHM; }
            set { _isoTHM = value; }
        }

        KineticRateReaction _kineticRateReaction;

        public KineticRateReaction KineticRateReaction
        {
            get { return _kineticRateReaction; }
            set { _kineticRateReaction = value; }
        }


     

        bool _isSpecifyIC=false;

        public bool IsSpecifyIC
        {
            get { return _isSpecifyIC; }
            set { _isSpecifyIC = value; }
        }


        #region 逐层输入部分

        List<double[,]> _bulkDensity;

        public List<double[,]> BulkDensity
        {
            get { return _bulkDensity; }
            set { _bulkDensity = value; }
        }

        List<double[,]> _porosity2;

        public List<double[,]> Porosity2
        {
            get { return _porosity2; }
            set { _porosity2 = value; }
        }






        List<ChemicalSpecies> _ChemicalSpecies;


        /// <summary>
        /// 对应于每一个污染物
        /// </summary>
        public List<ChemicalSpecies> ChemicalSpecies
        {
            get { return _ChemicalSpecies; }
            set { _ChemicalSpecies = value; }
        }



        List<string> _currentSpecies;

        public List<string> CurrentSpecies
        {
            get { return _currentSpecies; }

        }


        int _nSpecies;

        public int NSpecies
        {
            get { return _nSpecies; }

        }




        #endregion


        int _nrows;

        public int Nrows
        {
            get { return _nrows; }
           
        }
        int _ncols;

        public int Ncols
        {
            get { return _ncols; }
            
        }

        int _nlayers;

        public int Nlayers
        {
            get { return _nlayers; }
           
        }



        public void OnGridReset(int nrows, int ncols, int nlayers)
        {

            this._nrows = nrows;

            this._ncols = ncols;

            this._nlayers = nlayers;





            foreach (ChemicalSpecies item in this._ChemicalSpecies)
            {
                item.OnGridReset(nrows,ncols,nlayers);

            }

            this._bulkDensity.Clear();
            this._porosity2.Clear();
            for (int i = 0; i < this._nlayers; i++)
            {
                this._bulkDensity.Add(new double[this._nrows, this._ncols]);
                this._porosity2.Add(new double[this._nrows, this._ncols]);

            }


        }


        public void OnSpeciesReset(List<string> spNames)
        {

            this._nSpecies = spNames.Count;
            this._currentSpecies = spNames;

            this._ChemicalSpecies.Clear();


            foreach (string  item in spNames)
            {
                ChemicalSpecies cs = new ChemicalSpecies(item,this._nrows,this._ncols,this._nlayers);

                this._ChemicalSpecies.Add(cs);

                
            }
        


        }


        public ChemicalReactionPack ToPack()
        {
            ChemicalReactionPack chem = new ChemicalReactionPack();
            switch (this.IsoTHM)
            {
                case ISOTHMType.NOSorption:
                    chem.ISOTHM = 0;                   
                        chem.IGETSC = 0;                  
                    break;
                case ISOTHMType.LinearIsoTherm:
                    chem.ISOTHM = 1;
                    chem.IGETSC = 0;
                    break;
                case ISOTHMType.FreundlichIsoTherm:
                    chem.ISOTHM = 2;
                    chem.IGETSC = 0;
                    break;
                case ISOTHMType.LangmuirIsoTherm:
                    chem.ISOTHM = 3;
                        chem.IGETSC = 0;                   
                    break;
                case ISOTHMType.FirstOrderKineticSorption:
                    chem.ISOTHM = 4;
                    if (this.IsSpecifyIC)
                    {
                        chem.IGETSC = 1;
                    }
                    else
                    {
                        chem.IGETSC = 0;
                    }
                    break;
                case ISOTHMType.DualDomainTransferWithoutSorption:
                    chem.ISOTHM = 5;
                    if (this.IsSpecifyIC)
                    {
                        chem.IGETSC = 1;
                    }
                    else
                    {
                        chem.IGETSC = 0;
                    }
                    break;
                case ISOTHMType.DualDomainTransferWithSorption:
                    chem.ISOTHM = 6;
                    if (this.IsSpecifyIC)
                    {
                        chem.IGETSC = 1;
                    }
                    else
                    {
                        chem.IGETSC = 0;
                    }
                    break;
            }
            if (this.KineticRateReaction == KineticRateReaction.NONE)
            {
                chem.IREACT = 0;
            }
            else
            {
                chem.IREACT = 1;
            }

            chem.IRCTOP = 2;
            //if (this.IsSpecifyIC)
            //{
            //    chem.IGETSC = 1;
            //}
            //else
            //{
            //    chem.IGETSC = 0;
            //}

            chem.RHOB = this.BulkDensity;
            chem.PRSITY2 = this.Porosity2;

            for (int i = 0; i < this.ChemicalSpecies.Count; i++)
            {
                chem.SRCONC.Add(this.ChemicalSpecies[i].SrcConc);
            }
                   

            for (int i = 0; i < this.ChemicalSpecies.Count; i++)
            {
                chem.SP1.Add(this.ChemicalSpecies[i].Sp1);
            }

            for (int i = 0; i < this.ChemicalSpecies.Count; i++)
            {
                chem.SP2.Add(this.ChemicalSpecies[i].Sp2);
            }
            for (int i = 0; i < this.ChemicalSpecies.Count; i++)
            {
                chem.RC1.Add(this.ChemicalSpecies[i].Rc1);
            }
            for (int i = 0; i < this.ChemicalSpecies.Count; i++)
            {
                chem.RC2.Add(this.ChemicalSpecies[i].Rc2);
            }

            return chem;
        }
        




        public object Clone()
        {
            ChemicalReaction cr =(ChemicalReaction) this.MemberwiseClone();
            cr._bulkDensity = new List<double[,]>();
            foreach (double[,] item in this._bulkDensity)
            {
                cr._bulkDensity.Add((double[,])item.Clone());
                
            }
            cr._porosity2 = new List<double[,]>();
            foreach (double [,] item in this ._porosity2)
            {
                cr._porosity2.Add((double [,])item.Clone());  
            }
            cr._ChemicalSpecies = new List<ChemicalSpecies>();
            foreach (ChemicalSpecies item in this ._ChemicalSpecies)
            {
                cr._ChemicalSpecies.Add((ChemicalSpecies) item.Clone());
            }

            return cr;
         
        }
    }
}
